<template>
  <div>
    <el-form class="query-table-form" :inline="true">
      <el-form-item>
        <select-address
          v-model="params.addressCode"
          placeholder="地址查询"
          clearable
          :auto-top="true"
          @change="getData()"
        ></select-address>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="params.customerName"
          placeholder="企业名称查询"
          :clearable="true"
          @keyup.native.enter="getData()"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="params.corpName"
          placeholder="负责人查询"
          :clearable="true"
          @keyup.native.enter="getData()"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getData()">查询</el-button>
      </el-form-item>
      <!-- <el-form-item class="float-right">
        <el-button icon="el-icon-download"
                   type="primary"
                   :loading="exportLoading"
                   @click="exportExcel('公共库')">导出</el-button>
      </el-form-item> -->
    </el-form>

    <c-table border class="table-sm" v-loading="loading" :columns="tableColumns" :data="tableData">
      <template v-slot:address="scope">
        <div class="text-truncate" :title="scope.row.address">{{ scope.row.address }}</div>
      </template>
      <template v-slot:useDir="scope">
        <div class="text-truncate" :title="scope.row.funds_use_dir">
          {{ scope.row.funds_use_dir }}
        </div>
      </template>
    </c-table>

    <div class="query-pagination">
      <div></div>
      <el-pagination
        layout="total, prev, pager, next, jumper"
        :current-page.sync="params.index"
        :page-size="params.size"
        :total="tableCount"
        @current-change="getData"
      ></el-pagination>
    </div>
    <el-dialog
      :visible.sync="reportVisible"
      :close-on-click-modal="false"
      append-to-body
      top="30px"
      width="80%"
    >
      <el-tabs>
        <el-tab-pane label="项目信息">
          <project-info :related-data="viewRow"></project-info>
        </el-tab-pane>
        <el-tab-pane label="附件">
          <attach-view :queryId="queryId" :visible="reportVisible"></attach-view>
        </el-tab-pane>
      </el-tabs>
    </el-dialog>

    <approve-edit
      :confuse="true"
      :visible.sync="approveVisible"
      :form-data="formData"
      @success="getData()"
      :table-id="formId"
    ></approve-edit>
  </div>
</template>

<script>
// import { enumOrgClass } from '@/credit/plugins/enum'
// import CreditFile from '@/bank/components/creditFile/BankCredit'
import SelectAddress from '@/components/select/SelectAddress'
// import NewProject from '@/bank/views/program/New'
import AttachView from '@/credit/components/AttachView'
import { mapGetters } from 'vuex'
import ApproveEdit from '@/bank/views/business/library/ApproveEdit'
import { addressCut } from '@vimi/utils-tool'
import ProjectInfo from '@/bank/views/business/library/ProjectInfo'
export default {
  components: {
    SelectAddress,
    // CreditFile,
    AttachView,
    // NewProject,
    ApproveEdit,
    ProjectInfo,
  },
  data() {
    return {
      order: { create_time: 'desc' },
      params: {
        index: 1,
        size: 15,
        address_code: '',
        customer_name: '',
        corp_name: '',
      },
      loading: false,
      reportVisible: false,
      viewRow: {},
      queryId: '',
      tableCount: 0,
      tableData: [],
      tableColumns: [
        {
          attrs: { label: '项目名称', prop: 'customer_name', minWidth: '110px' },
          process: (v, row) => ({
            attrs: { class: 'project-history', attrs: { 'data-history': row.is_history } },
          }),
        },
        { attrs: { label: '地址', prop: 'address' }, slot: 'address' },
        { attrs: { label: '负责人', prop: 'corp_name', align: 'center' } },
        { attrs: { label: '联系电话', prop: 'mobile_no', align: 'center' } },
        { attrs: { label: '申请金额(万元)', prop: 'apply_funds', align: 'center' } },
        { attrs: { label: '资金用途', prop: 'funds_use_dir' }, slot: 'useDir' },
        { attrs: { label: '新建时间', prop: 'create_time', align: 'center' } },
        { attrs: { label: '提交人', prop: 'submit_person_name', align: 'center' } },
        {
          attrs: { label: '操作', align: 'center', width: 120 },
          operations: [
            {
              content: '受理',
              icon: 'fa fa-gavel',
              event: this.handleApprove,
              ifRender: row => +row.claim_status === 0,
            },
            { content: '查看', icon: 'fa fa-search', event: this.handleEditReport },
          ],
        },
      ],
      approveVisible: false,
      formData: {},
      formId: '',
    }
  },
  computed: {
    ...mapGetters(['clientId', 'org', 'user']),
    bankType() {
      return this.user.clientOrgClassType
    },
    queryAddressCode() {
      return this.user.queryAddressCode
    },
  },
  watch: {},
  mounted() {},
  created() {
    this.params.addressCode = this.queryAddressCode
    this.getData()
  },
  methods: {
    handleApprove(row = {}) {
      this.formId = row.customer_related_id || ''
      this.formData = { ...row }
      this.formData.gua_type = +this.formData.gua_type ? this.formData.gua_type : ''
      this.approveVisible = true
    },
    getData() {
      this.loading = true
      let str = ''
      Object.keys(this.params).forEach(key => {
        if (this.params[key]) {
          str +=
            key === 'addressCode'
              ? `&${key}=${addressCut(this.params[key])}`
              : `&${key}=${this.params[key]}`
        }
      })
      this.axios
        .get(
          `/api/gua/customer-related?apiKey=customer-related-public-query&publicBankIDs=${this.clientId}&bankTypes=${this.bankType}${str}`
        )
        .then(res => {
          this.tableData = res?.data?.data || []
          this.tableCount = res?.data?.totalCount || 0
        })
        .finally(() => {
          this.loading = false
        })
    },
    dataAdapter(item) {
      // if (this.org.orgCode.includes(item.bank_code) && +item.submit_type === 5) {
      //   item.project_type = '自建项目'
      // } else if (this.clientId.includes(item.public_bank_ids)) {
      //   item.project_type = '推荐项目'
      // } else {
      //   item.project_type = '受理项目'
      // }
      item.create_time = this.$utils.dateFormat(item.create_time, 'yyyy-MM-dd')
      return item
    },
    handleEditReport(row) {
      this.queryId = row.customer_related_id
      this.viewMode = true
      this.viewRow = row
      this.reportVisible = true
    },
  },
}
</script>
<style lang="scss" scoped></style>
